//
//  NIAlertViewController.m
//  gweAD
//
//  Created by navinfo air on 2017/2/13.
//  Copyright © 2017年 mengy. All rights reserved.
//

#import "NIAlertViewController.h"
#import "DeviceDefine.h"

@interface NIAlertViewController (){
    BOOL getFrame;
}
@property(nonatomic) UIImage *image;
@property(nonatomic) UIFont *titleFont;
@property(nonatomic) UIFont *messageFont;
@end

@implementation NIAlertViewController

- (void)setImageAndTitleFont:(UIImage *)image titleFont:(UIFont *)titleFont messageFont:(UIFont *)messageFont{
    _image = image;
    if (!titleFont) {
        _titleFont = [UIFont boldSystemFontOfSize:17.0f];
    }else{
        _titleFont = titleFont;
    }
    _messageFont = messageFont;
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    if (!getFrame) {
        if (_image) {
            NSString *tmpStr = self.title;
            UILabel *tmpLabel = [[UILabel alloc] init];
            tmpLabel.font = _titleFont;
            tmpLabel.numberOfLines = 0;
            
            do {
                tmpStr = [NSString stringWithFormat:@"\n%@",tmpStr];
                tmpLabel.text = tmpStr;
                [tmpLabel sizeToFit];
            } while (_image.size.height + 20 > tmpLabel.frame.size.height);
            
            self.title = tmpStr;
            
            UIImageView *imageView = [[UIImageView alloc] initWithImage:_image];
            [imageView setContentMode:UIViewContentModeScaleAspectFit];
            if (IOS_VERSION < 10.0f) {
                [imageView setFrame:CGRectMake(0, 10, 270, _image.size.height)];
            }else{
                [imageView setFrame:CGRectMake(0, 10, self.view.frame.size.width, _image.size.height)];
            }
            [self.view addSubview:imageView];
        }
        
        if (self.title) {
            NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:self.title];
            [titleText addAttribute:NSFontAttributeName value:_titleFont range:NSMakeRange(0, self.title.length)];
            [titleText addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, self.title.length)];
            [self setValue:titleText forKey:@"attributedTitle"];
        }
        getFrame = YES;
    }
}


@end
